//
//  GXKPhotoListViewController.m
//  GXKPhotos
//
//  Created by 乐善街 on 16/6/17.
//  Copyright © 2016年 tianchuanshangwu. All rights reserved.
//

#import "GXKPhotoListViewController.h"
#import "PhotoListTableViewCell.h"
#import "GXKListModel.h"
#import <Photos/Photos.h>
@implementation GXKPhotoListViewController{
    NSMutableArray *listModelarray;
    UITableView *_tabelView;

}
-(instancetype )initWithFrame:(CGRect)frame{
    self =[super initWithFrame:frame];
    if (self) {
          }
    return self;
}


-(void)show:(NSMutableArray *)dataArray{
    listModelarray=dataArray;
    _tabelView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 375, 557) style:UITableViewStylePlain];
    _tabelView.delegate=self;
    _tabelView.dataSource=self;
    [self addSubview:_tabelView];
   [[UIApplication sharedApplication].keyWindow addSubview:self];
    [_tabelView reloadData];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return listModelarray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"cell";
    PhotoListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell ==nil) {
        cell=[[NSBundle mainBundle]loadNibNamed:@"PhotoListTableViewCell" owner:nil options:nil].firstObject;
    }
    GXKListModel *listModel=listModelarray[indexPath.row];
    
    
    [[PHImageManager defaultManager] requestImageForAsset:listModel.lastObject targetSize:CGSizeMake(200, 200) contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info){
      
          cell.imageView.image= result;
        cell.imageView.contentMode=UIViewContentModeScaleAspectFill;
        cell.imageView.clipsToBounds=YES;
    }];
    
    cell.titleLabel.text=listModel.title;
    cell.numberLabel.text=[NSString stringWithFormat:@"%lu",listModel.count];
    return cell;



}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    _block(indexPath.row);
    [self removeFromSuperview];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80;
}


@end
